package com.eascs.partner.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.archive.entity.ArchiveLoanEntity;
import com.eascs.common.util.DateUtils;
import com.eascs.common.util.StringUtil;
import com.eascs.partner.dao.PartnerDao;
import com.eascs.partner.form.PartnerQueryForm;

@Service
public class PartnerService {
	public static final int status_delete=2;
	public static final int status_normal=1;
	@Autowired
	PartnerDao partnerDao;

	public PageResult<Object[]> findPartnerInfoByPage(PartnerQueryForm params, int pageIndex, int pageSize) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<Object[]>(Object[].class, pageSize, pageIndex);
		StringBuffer querySql = new StringBuffer();//查询语句拼接
		querySql.append(" SELECT P.ID,P.PARTNER_CODE,P.operator,P.ORG_PROVINCE,P.PARTNER_NAME,B.BRAND_NAME, ");
		querySql.append(" P.FINANCE_MONEY,DATE_FORMAT(P.CREATE_TIME , '%Y-%m-%d'),P.WF_STATUS , P.OPERATOR_ID ");
		querySql.append(" FROM O2O_PARTNER_INFO P ");
		querySql.append(" LEFT JOIN O2O_BRAND_INFO B ON P.BRAND_ID = B.ID");
		querySql.append(" WHERE /**DATA_PERMISSION(P.org_id)**/ 1=1 ");
		//按传入条件参数查询相关数据
		if (StringUtils.isNotBlank(params.getPartnerName())) {
			querySql.append(" AND P.PARTNER_NAME LIKE ? ");
			sqlPageQuery.addLikeParam(params.getPartnerName());
		}
		if (StringUtils.isNotBlank(params.getBrandName())) {
			querySql.append(" AND B.BRAND_NAME LIKE ? ");
			sqlPageQuery.addLikeParam(params.getBrandName());
		}
		
		if (!StringUtil.isEmpty(params.getCreateTime())) {
			querySql.append(" AND P.CREATE_TIME >= ? ");
			sqlPageQuery.addParam(params.getCreateTime());
		}
		
		if (!StringUtil.isEmpty(params.getCreateEndTime())) {
			querySql.append(" AND P.CREATE_TIME < ?");
			sqlPageQuery.addParam(DateUtils.formateStandDate(DateUtils.addDate(DateUtils.formateStandDate(params.getCreateEndTime()), 1)));
		}
		if (StringUtils.isNotBlank(params.getHostProvince())) {
			querySql.append(" AND P.ORG_PROVINCE_ID= ? ");
			sqlPageQuery.addParam(params.getHostProvince());
		}
		if (StringUtils.isNotBlank(params.getStatus())) {
			querySql.append(" AND P.WF_STATUS= ? ");
			sqlPageQuery.addParam(params.getStatus());
		}
		if(StringUtils.isNotBlank(params.getOperator())){
			querySql.append(" AND P.operator_id= ? ");
			sqlPageQuery.addParam(params.getOperator());
		}
		querySql.append(" AND p.STATUS = ? ");
		sqlPageQuery.addParam(status_normal);
		querySql.append(" ORDER BY P.CREATE_TIME DESC");
		sqlPageQuery.setQuerySql(querySql.toString());
		return partnerDao.findDataWithPage(sqlPageQuery);
	}
	
	
	/**
	 * 导出查询
	 * @param pageSize
	 * @param pageIndex
	 * @return
	 */
	public PageResult<Object[]> queryExportByPage(PartnerQueryForm params) {
		int pageIndex =  0;
		int pageSize = Integer.MAX_VALUE;
		
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<Object[]>(Object[].class, pageSize, pageIndex);
		StringBuffer querySql = new StringBuffer();//查询语句拼接
		querySql.append(" SELECT P.operator,P.ORG_PROVINCE,P.PARTNER_NAME,B.BRAND_NAME,P.scope_of_business, ");
		querySql.append(" P.FINANCE_MONEY,DATE_FORMAT(P.CREATE_TIME , '%Y-%m-%d'),");
		querySql.append(" CASE P.WF_STATUS WHEN -1 THEN '新建' WHEN 0 THEN '审核通过' WHEN 2 THEN '审核中' WHEN 3 THEN '驳回'  END  ");
		querySql.append(" FROM O2O_PARTNER_INFO P ");
		querySql.append(" LEFT JOIN O2O_BRAND_INFO B ON P.BRAND_ID = B.ID");
		querySql.append(" WHERE /**DATA_PERMISSION(P.org_id)**/ 1=1 ");
		//按传入条件参数查询相关数据
		if (StringUtils.isNotBlank(params.getPartnerName())) {
			querySql.append(" AND P.PARTNER_NAME LIKE ? ");
			sqlPageQuery.addLikeParam(params.getPartnerName());
		}
		if (StringUtils.isNotBlank(params.getBrandName())) {
			querySql.append(" AND B.BRAND_NAME LIKE ? ");
			sqlPageQuery.addLikeParam(params.getBrandName());
		}
		if (!StringUtil.isEmpty(params.getCreateTime())) {
			querySql.append(" AND P.CREATE_TIME >= ? ");
			sqlPageQuery.addParam(params.getCreateTime());
		}
		if (!StringUtil.isEmpty(params.getCreateEndTime())) {
			querySql.append(" AND P.CREATE_TIME < ?");
			sqlPageQuery.addParam(DateUtils.formateStandDate(DateUtils.addDate(DateUtils.formateStandDate(params.getCreateEndTime()), 1)));
		}
		if (StringUtils.isNotBlank(params.getHostProvince())) {
			querySql.append(" AND P.ORG_PROVINCE_ID= ? ");
			sqlPageQuery.addParam(params.getHostProvince());
		}
		if (StringUtils.isNotBlank(params.getStatus())) {
			querySql.append(" AND P.WF_STATUS= ? ");
			sqlPageQuery.addParam(params.getStatus());
		}
		if(StringUtils.isNotBlank(params.getOperator())){
			querySql.append(" AND P.operator= ? ");
			sqlPageQuery.addParam(params.getOperator());
		}
		querySql.append(" AND p.STATUS = ? ");
		sqlPageQuery.addParam(status_normal);
		querySql.append(" ORDER BY P.CREATE_TIME DESC");
		sqlPageQuery.setQuerySql(querySql.toString());
		return partnerDao.findDataWithPage(sqlPageQuery);

	}
	

}
